// CompletionListWindow.cs created with MonoDevelop
// User: kpr at 12:06 03.11.2007
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;
using Gtk;

namespace Designer
{

		
	public class CompletionListWindow : Gtk.Window
	{
		[Flags]
		public enum KeyAction { Process=1, Ignore=2, CloseWindow=4, Complete=8 }
	
		ICompletionWidget completionWidget;
		CompletionDelegate closedDelegate;
		ICodeCompletionContext completionContext;
		
		public CompletionListWindow() : base(Gtk.WindowType.Popup)
		{
			HBox hbox = new HBox();
			Label label = new Label("gml_test()");
			hbox.Add(label);
			this.Add(hbox);
		}
		public new void Show ()
		{
			this.ShowAll ();
			Reset ();
		}
		
		public void Reset ()
		{

		}
		
		public static void HideWindow ()
		{
			wnd.Hide ();
		}
		public new void Hide ()
		{
			base.Hide();
			if (closedDelegate != null) {
				closedDelegate ();
				closedDelegate = null;
			}
		}
		
		static CompletionListWindow wnd;
			
		static CompletionListWindow ()
		{
			wnd = new CompletionListWindow ();
		}
		
		public static void ShowWindow(ICompletionWidget completionWidget, ICodeCompletionContext completionContext, CompletionDelegate closedDelegate)
		{
			wnd.completionContext = completionContext;
			wnd.completionWidget = completionWidget;
			wnd.closedDelegate = closedDelegate;
			
			int x = completionContext.TriggerXCoord;
			int y = completionContext.TriggerYCoord;
			
			int w, h;
			wnd.GetSize (out w, out h);
			
			if ((x + w) > wnd.Screen.Width)
				x = wnd.Screen.Width - w;
			
			if ((y + h) > wnd.Screen.Height)
			{
				y = y - completionContext.TriggerTextHeight - h;
			}	
			
			wnd.Move (x, y);
			wnd.Show();
		}
		public KeyAction ProcessKey (Gdk.Key key, Gdk.ModifierType modifier)
		{
//			switch (key)
//			{
//				case Gdk.Key.Up:
//					list.Selection --;
//					return KeyAction.Ignore;
//					
//				case Gdk.Key.Down:
//					list.Selection ++;
//					return KeyAction.Ignore;
//					
//				case Gdk.Key.Page_Up:
//					list.Selection -= list.VisibleRows - 1;
//					return KeyAction.Ignore;
//					
//				case Gdk.Key.Page_Down:
//					list.Selection += list.VisibleRows - 1;
//					return KeyAction.Ignore;
//					
//				case Gdk.Key.Left:
//					//if (curPos == 0) return KeyAction.CloseWindow | KeyAction.Process;
//					//curPos--;
//					return KeyAction.Process;
//					
//				case Gdk.Key.BackSpace:
//					if (curPos == 0) return KeyAction.CloseWindow | KeyAction.Process;
//					curPos--;
//					word.Remove (curPos, 1);
//					UpdateWordSelection ();
//					return KeyAction.Process;
//					
//				case Gdk.Key.Right:
//					//if (curPos == word.Length) return KeyAction.CloseWindow | KeyAction.Process;
//					//curPos++;
//					return KeyAction.Process;
//					
//				case Gdk.Key.Tab:
//				case Gdk.Key.Return:
//				case Gdk.Key.ISO_Enter:
//				case Gdk.Key.Key_3270_Enter:
//				case Gdk.Key.KP_Enter:
//					return KeyAction.Complete | KeyAction.Ignore | KeyAction.CloseWindow;
//					
//				case Gdk.Key.Escape:
//					return KeyAction.CloseWindow | KeyAction.Ignore;
//					
//				case Gdk.Key.KP_Divide:
//				case Gdk.Key.slash:
//				case Gdk.Key.Home:
//				case Gdk.Key.End:
//					return KeyAction.CloseWindow | KeyAction.Process;
//					
//				case Gdk.Key.Control_L:
//				case Gdk.Key.Control_R:
//				case Gdk.Key.Alt_L:
//				case Gdk.Key.Alt_R:
//				case Gdk.Key.Shift_L:
//				case Gdk.Key.Shift_R:
//				case Gdk.Key.ISO_Level3_Shift:	// AltGr
//					return KeyAction.Process;
//			}
//			char c = (char)(uint)key;
//			
//			if (System.Char.IsLetterOrDigit (c) || c == '_') {
//				word.Insert (curPos++, c);
//				UpdateWordSelection ();
//				return KeyAction.Process;
//			} else if ((System.Char.IsPunctuation (c) || c == ' ') && !list.SelectionDisabled) {
//				return KeyAction.Complete | KeyAction.Process | KeyAction.CloseWindow;
//			}
			
			return KeyAction.CloseWindow | KeyAction.Process;
		}
		
		public static bool ProcessKeyEvent (Gdk.Key key, Gdk.ModifierType modifier)
		{
			if (!wnd.Visible) return false;
			
			KeyAction ka = wnd.ProcessKey (key, modifier);
			
			if ((ka & KeyAction.CloseWindow) != 0)
			{
				Console.WriteLine("Closing popup");
				wnd.Hide ();
			}
				
			if ((ka & KeyAction.Complete) != 0) {
				//wnd.UpdateWord ();
			}
			
			if ((ka & KeyAction.Ignore) != 0)
				return true;

			if ((ka & KeyAction.Process) != 0) {
				if (key == Gdk.Key.Left || key == Gdk.Key.Right) {
					if (modifier != 0) {
						wnd.Hide ();
						return false;
					}
//					if (wnd.declarationviewwindow.Multiple) {
//						if (key == Gdk.Key.Left)
//							wnd.declarationviewwindow.OverloadLeft ();
//						else
//							wnd.declarationviewwindow.OverloadRight ();
//						wnd.UpdateDeclarationView ();
//					}
					return true;
				}
			}

			return false;
		}
	}
	
	public delegate void CompletionDelegate ();
}
